﻿namespace TicTacToe.Core.AI
{
    static partial class Conditions
    {
        static ConditionResult EnemyXTwoRow(AIGameField field, int player, int x, int y, int z)
        {
            var other = player == 1 ? 2 : 1;

            if (x == 0)
            {
                if (field[x + 1, y, z] == other &&
                    field[x + 2, y, z] == other &&
                    field[x + 3, y, z] == 0)
                    return ConditionResult.NextToEnemyTwoRow;
            }
            else if (x == 1)
            {
                if (field[x + 1, y, z] == other &&
                    field[x + 2, y, z] == other &&
                    field[x - 1, y, z] == 0)
                    return ConditionResult.NextToEnemyTwoRow;
            }
            else if (x == 2)
            {
                if (field[x - 1, y, z] == other &&
                    field[x - 2, y, z] == other &&
                    field[x + 1, y, z] == 0)
                    return ConditionResult.NextToEnemyTwoRow;
            }
            else if (x == 3)
            {
                if (field[x - 1, y, z] == other &&
                    field[x - 2, y, z] == other &&
                    field[x - 3, y, z] == 0)
                    return ConditionResult.NextToEnemyTwoRow;
            }
            return ConditionResult.None;
        }

        static ConditionResult EnemyYTwoRow(AIGameField field, int player, int x, int y, int z)
        {
            var other = player == 1 ? 2 : 1;

            if (y == 0)
            {
                if (field[x, y + 1, z] == other &&
                    field[x, y + 2, z] == other &&
                    field[x, y + 3, z] == 0)
                    return ConditionResult.NextToEnemyTwoRow;
            }
            else if (y == 1)
            {
                if (field[x, y + 1, z] == other &&
                    field[x, y + 2, z] == other &&
                    field[x, y - 1, z] == 0)
                    return ConditionResult.NextToEnemyTwoRow;
            }
            else if (y == 2)
            {
                if (field[x, y - 1, z] == other &&
                    field[x, y - 2, z] == other &&
                    field[x, y + 1, z] == 0)
                    return ConditionResult.NextToEnemyTwoRow;
            }
            else if (y == 3)
            {
                if (field[x, y - 1, z] == other &&
                    field[x, y - 2, z] == other &&
                    field[x, y - 3, z] == 0)
                    return ConditionResult.NextToEnemyTwoRow;
            }
            return ConditionResult.None;
        }

        static ConditionResult EnemyZTwoRow(AIGameField field, int player, int x, int y, int z)
        {
            var other = player == 1 ? 2 : 1;

            if (z == 0)
            {
                if (field[x, y, z + 1] == other &&
                    field[x, y, z + 2] == other &&
                    field[x, y, z + 3] == 0)
                    return ConditionResult.NextToEnemyTwoRow;
            }
            else if (z == 1)
            {
                if (field[x, y, z + 1] == other &&
                    field[x, y, z + 2] == other &&
                    field[x, y, z - 1] == 0)
                    return ConditionResult.NextToEnemyTwoRow;
            }
            else if (z == 2)
            {
                if (field[x, y, z - 1] == other &&
                    field[x, y, z - 2] == other &&
                    field[x, y, z + 1] == 0)
                    return ConditionResult.NextToEnemyTwoRow;
            }
            else if (z == 3)
            {
                if (field[x, y, z - 1] == other &&
                    field[x, y, z - 2] == other &&
                    field[x, y, z - 3] == 0)
                    return ConditionResult.NextToEnemyTwoRow;
            }
            return ConditionResult.None;
        }

        static ConditionResult EnemyXYTwoRow(AIGameField field, int player, int x, int y, int z)
        {
            var other = player == 1 ? 2 : 1;

            if (x == 0 && y == 0)
            {
                if (field[x + 1, y + 1, z] == other &&
                    field[x + 2, y + 2, z] == other &&
                    field[x + 3, y + 3, z] == 0)
                    return ConditionResult.NextToEnemyTwoRow;
            }
            else if (x == 0 && y == 3)
            {
                if (field[x + 1, y - 1, z] == other &&
                    field[x + 2, y - 2, z] == other &&
                    field[x + 3, y - 3, z] == 0)
                    return ConditionResult.NextToEnemyTwoRow;
            }
            else if (x == 3 && y == 0)
            {
                if (field[x - 1, y + 1, z] == other &&
                    field[x - 2, y + 2, z] == other &&
                    field[x - 3, y + 3, z] == 0)
                    return ConditionResult.NextToEnemyTwoRow;
            }
            else if (x == 3 && y == 3)
            {
                if (field[x - 1, y - 1, z] == other &&
                    field[x - 2, y - 2, z] == other &&
                    field[x - 3, y - 3, z] == 0)
                    return ConditionResult.NextToEnemyTwoRow;
            }
            else if (x == 1 && y == 1)
            {
                if (field[x + 1, y + 1, z] == other &&
                    field[x + 2, y + 2, z] == other &&
                    field[x - 1, y - 1, z] == 0)
                    return ConditionResult.NextToEnemyTwoRow;
            }
            else if (x == 1 && y == 2)
            {
                if (field[x + 1, y - 1, z] == other &&
                    field[x + 2, y - 2, z] == other &&
                    field[x - 1, y + 1, z] == 0)
                    return ConditionResult.NextToEnemyTwoRow;
            }
            else if (x == 2 && y == 1)
            {
                if (field[x - 1, y + 1, z] == other &&
                    field[x - 2, y + 2, z] == other &&
                    field[x + 1, y - 1, z] == 0)
                    return ConditionResult.NextToEnemyTwoRow;
            }
            else if (x == 2 && y == 2)
            {
                if (field[x - 1, y - 1, z] == other &&
                    field[x - 2, y - 2, z] == other &&
                    field[x + 1, y + 1, z] == 0)
                    return ConditionResult.NextToEnemyTwoRow;
            }

            return ConditionResult.None;
        }

        static ConditionResult EnemyXZTwoRow(AIGameField field, int player, int x, int y, int z)
        {
            var other = player == 1 ? 2 : 1;

            if (x == 0 && z == 0)
            {
                if (field[x + 1, y, z + 1] == other &&
                    field[x + 2, y, z + 2] == other &&
                    field[x + 3, y, z + 3] == 0)
                    return ConditionResult.NextToEnemyTwoRow;
            }
            else if (x == 0 && z == 3)
            {
                if (field[x + 1, y, z - 1] == other &&
                    field[x + 2, y, z - 2] == other &&
                    field[x + 3, y, z - 3] == 0)
                    return ConditionResult.NextToEnemyTwoRow;
            }
            else if (x == 3 && z == 0)
            {
                if (field[x - 1, y, z + 1] == other &&
                    field[x - 2, y, z + 2] == other &&
                    field[x - 3, y, z + 3] == 0)
                    return ConditionResult.NextToEnemyTwoRow;
            }
            else if (x == 3 && z == 3)
            {
                if (field[x - 1, y, z - 1] == other &&
                    field[x - 2, y, z - 2] == other &&
                    field[x - 3, y, z - 3] == 0)
                    return ConditionResult.NextToEnemyTwoRow;
            }
            else if (x == 1 && z == 1)
            {
                if (field[x + 1, y, z + 1] == other &&
                    field[x + 2, y, z + 2] == other &&
                    field[x - 1, y, z - 1] == 0)
                    return ConditionResult.NextToEnemyTwoRow;
            }
            else if (x == 1 && z == 2)
            {
                if (field[x + 1, y, z - 1] == other &&
                    field[x + 2, y, z - 2] == other &&
                    field[x - 1, y, z + 1] == 0)
                    return ConditionResult.NextToEnemyTwoRow;
            }
            else if (x == 2 && z == 1)
            {
                if (field[x - 1, y, z + 1] == other &&
                    field[x - 2, y, z + 2] == other &&
                    field[x + 1, y, z - 1] == 0)
                    return ConditionResult.NextToEnemyTwoRow;
            }
            else if (x == 2 && z == 2)
            {
                if (field[x - 1, y, z - 1] == other &&
                    field[x - 2, y, z - 2] == other &&
                    field[x + 1, y, z + 1] == 0)
                    return ConditionResult.NextToEnemyTwoRow;
            }

            return ConditionResult.None;
        }

        static ConditionResult EnemyYZTwoRow(AIGameField field, int player, int x, int y, int z)
        {
            var other = player == 1 ? 2 : 1;

            if (y == 0 && z == 0)
            {
                if (field[x, y + 1, z + 1] == other &&
                    field[x, y + 2, z + 2] == other &&
                    field[x, y + 3, z + 3] == 0)
                    return ConditionResult.NextToEnemyTwoRow;
            }
            else if (y == 0 && z == 3)
            {
                if (field[x, y + 1, z - 1] == other &&
                    field[x, y + 2, z - 2] == other &&
                    field[x, y + 3, z - 3] == 0)
                    return ConditionResult.NextToEnemyTwoRow;
            }
            else if (y == 3 && z == 0)
            {
                if (field[x, y - 1, z + 1] == other &&
                    field[x, y - 2, z + 2] == other &&
                    field[x, y - 3, z + 3] == 0)
                    return ConditionResult.NextToEnemyTwoRow;
            }
            else if (y == 3 && z == 3)
            {
                if (field[x, y - 1, z - 1] == other &&
                    field[x, y - 2, z - 2] == other &&
                    field[x, y - 3, z - 3] == 0)
                    return ConditionResult.NextToEnemyTwoRow;
            }
            else if (y == 1 && z == 1)
            {
                if (field[x, y + 1, z + 1] == other &&
                    field[x, y + 2, z + 2] == other &&
                    field[x, y - 1, z - 1] == 0)
                    return ConditionResult.NextToEnemyTwoRow;
            }
            else if (y == 1 && z == 2)
            {
                if (field[x, y + 1, z - 1] == other &&
                    field[x, y + 2, z - 2] == other &&
                    field[x, y - 1, z + 1] == 0)
                    return ConditionResult.NextToEnemyTwoRow;
            }
            else if (y == 2 && z == 1)
            {
                if (field[x, y - 1, z + 1] == other &&
                    field[x, y - 2, z + 2] == other &&
                    field[x, y + 1, z - 1] == 0)
                    return ConditionResult.NextToEnemyTwoRow;
            }
            else if (y == 2 && z == 2)
            {
                if (field[x, y - 1, z - 1] == other &&
                    field[x, y - 2, z - 2] == other &&
                    field[x, y + 1, z + 1] == 0)
                    return ConditionResult.NextToEnemyTwoRow;
            }

            return ConditionResult.None;
        }
    }
}
